// 该文件用于创建Vuex中最为核心的store
import Vuex from 'vuex'
import Vue from 'vue'
import {updatelike} from '../utils/updateLike.js'
// 引入Vuex
Vue.use(Vuex)
// 准备actions用于响应组件中的动作
const actions = {
}

// 准备mutations用于操作数据state
const mutations = {
    changeSkin(state, val) {
        state.skin = val
    },
    changeshow(state, val) {
        state.showlogin = val
    },
    userinfor(state, val) {
        state.userbasic = val
    },
    tologin(state, val) {
        state.islogin = val
    },
    addInfomation(state, val) {
        state.infomation = val
    },
    initDynamic(state, val) {
        state.dynamics = [...val]
    },
    addDynamic(state, val) {
        state.dynamics = [...state.dynamics, ...val]
        state.dynamic_id++
    },
    setDynamicId(state, val) {
        state.dynamic_id = Number(val)
    },
    setLikes(state, val) {
    /** 
     * likeArr设计
     * 二维数组 or 数组对象
     * [
     *  {
     *      id:"",
     *      userid:"",
     *      like : ""
     *  }
     * ]
    */
        let like = state.whoLikes
        for (let i = 0; i < like.length; i++) {
            if (like[i].id == val.id) {
                like.splice(i, 1)
                return
            }
        }
        like.push(val)
    },
    sendLikes(state){
        // 这里发送请求 并清空存放值
        updatelike(state.whoLikes)
        state.whoLikes = []
    },
    addBlogs(state,val){
        state.blogs = [...state.blogs, ...val]
        state.blogs_id++
    },
    setBlogId(state, val) {
        state.blogs_id = Number(val)
    },
    initBlogs(state,val){
        state.blogs = [...val]
    },
    setNowblog(state,val){
        state.nowblog = val
    },
    BlogFinish(state){
        state.blogFinish = 1
    },
    CommentFinish(state){
        state.commentFinish = 1
    },
    adduserdynamics(state,val){
        state.userdynamics = val
    },
    addcollect(state,val){
        console.log('加入了',val.length);
        state.collect = val
    },
    addfooterTip(state,val){
        state.footerTip = val
    },
}

// 准备state用于存储数据
const state = {
    skin: true,
    islogin: false,
    showlogin: false,
    userbasic: {},
    infomation: {},
    dynamics: [],
    dynamic_id: 1, // 用于存放动态分页到了第几页
    whoLikes: [], // 用于存储用户点击了那些评论,在退出动态详情or切换博客时清空数组并发送请求(退出时发送并清空即可)
    blogs:[],
    blogs_id:1,
    nowblog:{},
    blogFinish:0,    // zy  :  数据库中无更多博客数据
    commentFinish:0, // lxj ： 数据库中无更多动态数据
    blog_list:'blog_list',
    blog_context:"blog_context",
    userdynamics:[],
    footerTip:'加载中~~',
    collect:[]
}

// 创建store
export default new Vuex.Store({
    actions,
    mutations,
    state
})